function recordChapter()
{
	selectedTask=null;
	for (var i=0; i<recordTaskList.count; i++)
	{
		if (recordTaskList.objects[i]["chapter.chapterId"]==selectedChapterId)
		{
			selectedTask=recordTaskList.objects[i];
			break;
		}
	}

	checkPermission();
}

function checkPermission()
{
	var publicFlag:String;
	var collabFlag:String;
	var memberId:String;
	var groupId:String;

	for (var i=0; i<recordAlbumDetails.count; i++)
	{
		var albumDetail=recordAlbumDetails.objects[i];
		if (albumDetail.recordAlbumId==selectedAlbumId)
		{
			publicFlag=albumDetail.publicFlag.toString();
			collabFlag=albumDetail.collabFlag.toString();
			memberId=albumDetail["member.memberId"].toString();
			groupId=albumDetail.publicGroupId.toString();
			break;
		}
	}

	if (publicFlag==null)
	{
		Packages.modula.MessageCenter.send("NO_ACCESS", 1, 0);
		return;
	}

	if (memberId==Packages.model.UserProfile.data.memberId)
	{
		doRecordChapter(collabFlag, "ENTER_RECORDING_AS_OWNER", {returnCode:0});
		return;
	}

	if (publicFlag=="2")
	{
		checkFriends(memberId, collabFlag);
	}
	else if (publicFlag=="3")
	{
		checkGroupMembers(groupId, collabFlag);
	}
}

function checkGroupMembers(groupId:String, collabFlag:String)
{
	var obj:Object=new Object();
	obj.memberId=Packages.model.UserProfile.data.memberId.toString();
	obj.groupId=groupId.toString();

	var data:Object=new Object();
	data.invokeCall="doRecordChapter";
	data.invokeArgs=new Array();
	data.invokeArgs.push(collabFlag);
	data.invokeArgs.push("ENTER_RECORDING_AS_GROUPMEMBER");
	data.returnResult=true;

	Packages.modula.RequestCommand.request("REMOTING", "isMemberOfGroup", obj, null, null, this, data);
}

function checkFriends(memberId:String, collabFlag:String)
{
	var obj:Object=new Object();
	obj.memberIdTo=memberId.toString();
	obj.memberIdFrom=Packages.model.UserProfile.data.memberId.toString();

	var data:Object=new Object();
	data.invokeCall="doRecordChapter";
	data.invokeArgs=new Array();
	data.invokeArgs.push(collabFlag);
	data.invokeArgs.push("ENTER_RECORDING_AS_FRIEND");
	data.returnResult=true;

	Packages.modula.RequestCommand.request("REMOTING", "isFriend", obj, null, null, this, data);
}

function doRecordChapter(collabFlag:String, message:String, result:Object)
{
	Packages.log("doRecordChapter: "+collabFlag+" "+result.returnCode, "debug");
	Packages.modula.MessageCenter.send(message, 1, 0);
	if (result.returnCode!=0)
	{
		Packages.modula.MessageCenter.send("NO_RIGHTS_ACCESS", 1, 0);
		return;
	}
	if (selectedAlbumId!=null && selectedChapterId!=null)
	{
		if (collabFlag=="2")
		{
			var roomId=selectedAlbumId+"_"+selectedChapterId;
			Packages.model.UserProfile.RecordingCollabId=roomId;
		}
		Packages.modula.task.Composition.currentTask=selectedTask;
		Packages.components.SceneAgent.loadScene("content/myReadjoy/recording/Prepairation");
	}
}